function* draw(cards) {
    const c = [...cards];

    for (let i = c.length; i > 0; i--) {
        const pIdx = Math.floor(Math.random() * i);
        [c[pIdx], c[i - 1]] = [c[i - 1], c[pIdx]];
        yield c[i - 1];
    }
}

function generate(amount, count) {
    if (count <= 1) return [amount];
    const cards = Array(amount - 1).fill(0).map((_, i) => i + 1);
    const pick = draw(cards);
    const result = [];
    for (let i = 0; i < count; i++) {
        result.push(pick.next().value);
    }
    result.sort((a, b) => a - b);
    for (let i = count - 1; i > 0; i--) {
        result[i] = result[i] - result[i - 1];
    }
    return result;
}


let amountEl = document.getElementById('amount');
let countEl = document.getElementById('count');
let ul = document.getElementById('result');
let generateBtn = document.getElementById('generateBtn');

generateBtn.onclick = () => {
    let amount = Math.round(parseFloat(amountEl.value * 100 ));
    let count = parseInt(countEl.value);
    let output = [];
    if (isNaN(amount) || isNaN(count) || amount <= 0 || count <= 0) {
        output.push('输入格式不正确');
    } else if (amount < count) {
        output.push("钱不够分")
    } else {
        output.push(...generate(amount, count))
        output = output.map(m => (m / 100).toFixed(2))
    }
    ul.innerHTML = '<li>' + output.join('</li><li>') + '</li>';
}